package com.github.postsanf.yinian.fragment;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNTodayM;
import com.github.postsanf.yinian.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;


public class TodayShowFragment extends Fragment {
    public static final String[] mouthlist = new String[]{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
    private TextView splash_day;
    private TextView splash_mouth;
    private TextView splash_year;
    private ImageView iv_today_pic;
    private TextView iv_today_text;
    private Context content;
    private YNTodayM ynTodayM;
    private int daynum;

    public TodayShowFragment(Context content, YNTodayM ynTodayM, int daynum) {
        super();
        this.content = content;
        this.ynTodayM = ynTodayM;
        this.daynum = daynum;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.yn_frg_todayshow, container, false);
        iv_today_text = (TextView) view.findViewById(R.id.iv_today_text);
        iv_today_pic = (ImageView) view.findViewById(R.id.iv_today_pic);
        Glide.with(content).load(ynTodayM.getTMpic()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(iv_today_pic);
        splash_day = ((TextView) view.findViewById(R.id.splash_day));
        splash_mouth = ((TextView) view.findViewById(R.id.splash_mouth));
        splash_year = ((TextView) view.findViewById(R.id.splash_year));
        iv_today_text.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "MFKeSong-Regular.ttf"));
        if (!StringUtils.isEmpty(ynTodayM.getTMtext())) {
            iv_today_text.setText(ynTodayM.getTMtext());
        }
        splash_day.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "splash_day.ttf"));
        splash_year.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "splash_day.ttf"));
        splash_mouth.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "splash_mouth.ttf"));
        if (daynum == 0) {
            Date yesterdate = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(yesterdate);
            splash_year.setText(yesterday.split("-")[0]);
            splash_mouth.setText(mouthlist[yesterdate.getMonth()]);
            splash_day.setText(yesterday.split("-")[2]);
        } else {
            Date mdate = new Date(new Date().getTime());
            String str = new SimpleDateFormat("yyyy-MM-dd").format(mdate);
            splash_year.setText(str.split("-")[0]);
            splash_mouth.setText(mouthlist[mdate.getMonth()]);
            splash_day.setText(str.split("-")[2]);
        }
        return view;
    }
}
